package nccloud.web.hrppl.proupdate.proupdatehvo.action;

import java.util.ArrayList;
import java.util.HashMap;
//import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.borland.dx.sql.dataset.cons.DatabaseStringBean;

//import nccloud.framework.web.ui.pattern.grid.Grid;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.hrppl.proupdate.CGUpdateCVO;
import nc.vo.hrppl.proupdate.ProUpdateBVO;
import nc.vo.hrppl.proupdate.ProUpdateHVO;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.VOStatus;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;
import nccloud.web.codeplatform.framework.action.base.BaseAction;
import nc.bs.framework.json.core.exception.ExceptionUtils;
import nccloud.web.codeplatform.framework.action.base.RequstParamWapper;
import nccloud.web.codeplatform.framework.action.base.ResultWapper;
import nccloud.web.codeplatform.framework.action.base.RequestDTO;
import nccloud.web.codeplatform.framework.action.base.VOTransform;
import nccloud.web.hrppl.pjapplication.pjappl.action.TranslateActionUtil;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.core.util.ArrayUtils;
import nccloud.framework.web.container.IRequest;
import nccloud.framework.web.ui.pattern.extbillcard.ExtBillCard;
import nc.itf.hrppl.proupdate.proupdatehvo.IProUpdateHVOCheckService;
import nc.itf.hrppl.proupdate.proupdatehvo.IProUpdateHVOService;
import nc.vo.hrppl.proupdate.AggProUpdateHVO;
/**
 * 保存action（主子、单表、单form）
 */
public class SaveProUpdateHVOAction extends BaseAction{

	@Override
	public Object doAction(IRequest request, RequstParamWapper paramWapper) throws Throwable{
		RequestDTO param = VOTransform.toVO(paramWapper.requestString,RequestDTO.class);
		String procycle = String.valueOf(param.getBillCard().getHead().getValue("procycle"));
		if (procycle.contains(".")) {
			param.getBillCard().getHead().setValue("procycle", Integer.parseInt(procycle.split("\\.")[0]));
		}
		String pjappltype = String.valueOf(param.getBillCard().getHead().getValue("pjappltype"));
		if(pjappltype.contains(".")) {
			param.getBillCard().getHead().setValue("pjappltype", Integer.parseInt(pjappltype.split("\\.")[0]));
		}
		
		IProUpdateHVOService service = ServiceLocator.find(IProUpdateHVOService.class);

		AggProUpdateHVO[] vos = this.getVOs(param, AggProUpdateHVO.class);
		
		try {
			IProUpdateHVOCheckService checkService = ServiceLocator.find(IProUpdateHVOCheckService.class);
			checkService.checkNotUpdateAggProUpdateHVO(vos);
			if(!Objects.isNull(vos)){
				for (AggProUpdateHVO aggvo : vos) {
					ProUpdateHVO hvo = aggvo.getParentVO();
					ProUpdateBVO[] zjvos = (ProUpdateBVO[])aggvo.getChildren(ProUpdateBVO.class);
					UFDouble tzmonery = new UFDouble(0);
					if (ArrayUtils.isNotEmpty(zjvos)) {
						for (ProUpdateBVO zjvo : zjvos) { // updaftermoney
							UFDouble updaftermoney = zjvo.getUpdaftermoney();
							tzmonery = tzmonery.add(updaftermoney);
						}
					}// updallfun
					hvo.setUpdallfun(tzmonery);
					hvo.setAttributeValue("updallfun", tzmonery);
					String pk_proapp = hvo.getPk_proapp();
					// 去除采购页签空白行
					CGUpdateCVO[] cgvos =  (CGUpdateCVO[])aggvo.getChildren(CGUpdateCVO.class);
					IUAPQueryBS bs = ServiceLocator.find(IUAPQueryBS.class);
					if(ArrayUtils.isNotEmpty(cgvos)){
						ArrayList<CGUpdateCVO> list = new ArrayList<>();
						for(CGUpdateCVO cgvo : cgvos){
							Object vbdef1 = cgvo.getAttributeValue("vbdef1");
							if(vbdef1 == null && cgvo.getStatus() == 0){
								cgvo.setStatus(VOStatus.DELETED);
								list.add(cgvo);
							}else if(vbdef1 == null && cgvo.getStatus() == 2){
								continue;
							}else{
								cgvo.setStatus(VOStatus.NEW);
								list.add(cgvo);
							}
						}
						aggvo.setChildren(CGUpdateCVO.class, list.toArray(new CGUpdateCVO[list.size()]));
					}
				}

			}
		}catch(Throwable e) {
			throw new Throwable(e);
//			ExceptionUtils.wrapException(e);
		}
		vos = service.saveAggProUpdateHVO(vos[0]);
		Object result = buildResult(param,param.getBillCard().getHead()!=null,null,vos);


		//start modify by wy at 2024年7月26日
		ResultWapper o = (ResultWapper) result;
		ExtBillCard billcard = (ExtBillCard) o.getData();
		TranslateActionUtil.translateAggvoVO(billcard,"CGUpdateCVOSub");
		return result;
	
	}
	
}